//
//  ShowViewController.m
//  LoLBox
//
//  Created by lanou3g on 15/5/30.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ShowViewController.h"

@interface ShowViewController ()
@property (nonatomic , strong) NSMutableDictionary * dataDic;
@property (nonatomic , strong) NSMutableDictionary * dict;
@end

@implementation ShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self parseUrl];
    
    [self sendView];
    
    [self sendImage];
    
    
  //  [self PopoverC];
    
    
    [self settings];
    
    [self sendScroll];
    
    
}

-(void)sendScroll
{
    
    //切记写这个
    self.scrollerView.contentSize = CGSizeMake(2*375, 375);
    
    self.scrollerView.bounces = NO;
    
    self.scrollerView.showsHorizontalScrollIndicator = NO;
    
    self.scrollerView.showsVerticalScrollIndicator = NO;
    
    self.scrollerView.pagingEnabled = YES;//分页效果

    self.scrollerView.delegate = self;
    
    
}

#pragma mark-------实现UIScrollView中的delegate
//滚动停止时触发
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
     NSInteger index = self.scrollerView.contentOffset.x/self.scrollerView.frame.size.width;
    
    _segment.selectedSegmentIndex = index;
}




-(void)settings
{
    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"qingxin"]];
    
    
    [self.view addSubview:imageView];
    
    imageView.frame = CGRectMake(0, 0, 375, 665);;

    [self.view sendSubviewToBack:imageView];
}

-(void)PopoverC
{
    
    UIViewController * viewC = [[UIViewController alloc]init];
    
    viewC.view.frame= CGRectMake(0, 0, 100, 100);
    
    viewC.view.backgroundColor = [UIColor orangeColor];
    
    UIBarButtonItem * rightBI = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRedo target:self action:@selector(way)];
    
//将UIViewController的modalPresentationStyle设置成UIModalPresentationPopover，这个值用来实现popover效果，并且各个平台自动适应。第二行中，通过popoverPresentationController属性来获取它的popoverPresentationController，而不是创建一个新的。然后设置它的一些界面属性，最后调用presentViewController方法来显示这个controller。
    
    viewC.modalPresentationStyle = UIModalPresentationPopover;
    
    UIPopoverPresentationController * popVc = self.popoverPresentationController;
    
    popVc.barButtonItem = rightBI;
    
    popVc.permittedArrowDirections = UIPopoverArrowDirectionAny;
    
    popVc.delegate = self;

    [self presentViewController:viewC animated:YES completion:^{
        nil;
    }];
    
    self.navigationItem.rightBarButtonItem = rightBI;
}

-(void)way
{



}


-(void)parseUrl
{
    self.dataDic = [NSMutableDictionary dictionary];
    
    self.dict = [NSMutableDictionary dictionary];

    
    NSString * strUrl = [NSString stringWithFormat:@"http://ossweb-img.qq.com/upload/qqtalk/lol_hero/hero_%@.js",_str];
    
    NSURL * url = [NSURL URLWithString:strUrl];

   // NSLog(@"%@",url);
    
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    
    __weak ShowViewController * showVc = self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        showVc.dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        [showVc sendImage];
        [showVc sendView];
        
        NSArray * array = [_dataDic objectForKey:@"play_list"];
        
        for (_dict in array) {
            
          //  NSLog(@"%@",_dict);
            [self setOtherImage];
            
        }
    
        
           }];

}
-(void)setOtherImage
{
    self.image11.image = [UIImage imageNamed:[_dict objectForKey:@"bad1"]];
    
    NSLog(@"---%@",[_dict objectForKey:@"bad2"]);
    self.image22.image = [UIImage imageNamed:[_dict objectForKey:@"bad2"]];
    
    self.image33.image = [UIImage imageNamed:[_dict objectForKey:@"bad3"]];
    
    self.image44.image = [UIImage imageNamed:[_dict objectForKey:@"bad4"]];
    
    self.image55.image = [UIImage imageNamed:[_dict objectForKey:@"bad5"]];
    
    self.image66.image = [UIImage imageNamed:[_dict objectForKey:@"bad6"]];

    self.label1.text = [_dict objectForKey:@"bad_rune"];
}


-(void)sendView
{

    self.name.text =[_dataDic objectForKey:@"name"];
   
    self.tag1.text =[_dataDic objectForKey:@"tag1"];

    self.tag2.text =[_dataDic objectForKey:@"tag2"];

    self.tag3.text =[_dataDic objectForKey:@"tag3"];

    self.coin.text =[_dataDic objectForKey:@"coin"];
    
    self.money.text =[_dataDic objectForKey:@"money"];
    
    self.op_skill.text =[_dataDic objectForKey:@"op_skill"];
    
    self.teamwork.text =[_dataDic objectForKey:@"teamwork"];
    
}

-(void)sendImage
{


    
    NSString * str1 = [_dataDic objectForKey:@"skill1"];
    
    NSArray * strArray1 = [str1 componentsSeparatedByString:@"|"];
    
    NSString * newStr1 = [strArray1 objectAtIndex:0];
    
    self.image1.image = [UIImage imageNamed:newStr1];

    
    
    
    
    NSString * str2 = [_dataDic objectForKey:@"skill2"];
    
    NSArray * strArray2 = [str2 componentsSeparatedByString:@"|"];
    
    NSString * newStr2 = [strArray2 objectAtIndex:0];
    
    self.image2.image = [UIImage imageNamed:newStr2];
    
    
    NSString * str3 = [_dataDic objectForKey:@"skill3"];
    
    NSArray * strArray3 = [str3  componentsSeparatedByString:@"|"];
    
    NSString * newStr3 = [strArray3 objectAtIndex:0];
    
    self.image3.image = [UIImage imageNamed:newStr3];
    
    
    NSString * str4 = [_dataDic objectForKey:@"skill4"];
    
    NSArray * strArray4 = [str4 componentsSeparatedByString:@"|"];
    
    NSString * newStr4 = [strArray4 objectAtIndex:0];
    
    self.image4.image = [UIImage imageNamed:newStr4];
    
    
    
    NSString * str5 = [_dataDic objectForKey:@"skill5"];
    
    NSArray * strArray5 = [str5 componentsSeparatedByString:@"|"];
    
    NSString * newStr5 = [strArray5 objectAtIndex:0];
    
    self.image5.image = [UIImage imageNamed:newStr5];



}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)segment:(id)sender {
    
    if (self.segment.selectedSegmentIndex == 0) {
        
        self.scrollerView.contentOffset = CGPointMake(0, 0);
      //  [self.scrollerView setContentOffset:CGPointMake(0, 0) animated:YES];

        
    }
    
    if (self.segment.selectedSegmentIndex == 1) {
        
        //设置偏移量
        self.scrollerView.contentOffset = CGPointMake(375, 0);
    
      
    }
    
    
    
    
}
@end
